package com.stores.action;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.KeyFactory;
import com.stores.model.Db;
import com.stores.model.Product;
import com.stores.viewModel.ProductSelectViewModel;

public class ManageProductAction {

	/**
	 * 
	 * @param category
	 */
	public void Create(Product product) {
		PersistenceManager pm = Db.getPersistenceManager();
		try {
			pm.makePersistent(product);
		} finally {
			pm.close();
		}
	}

	/**
	 * 
	 * @param category
	 * @return
	 */
	public boolean Update(Product product, long id) {
		PersistenceManager pm = Db.getPersistenceManager();

		try {
			Product productData = pm.getObjectById(Product.class,
					KeyFactory.createKey(Product.class.getSimpleName(), id));
			productData.setName(product.getName());
			productData.setBrandId(product.getBrandId());
			productData.setCategoryIds(product.getCategoryIds());
			productData.setCostPrice(product.getCostPrice());
			productData.setLongDescription(product.getLongDescription());
			productData.setShortDescription(product.getShortDescription());
			productData.setUnitsInStock(product.getUnitsInStock());
			// productData.setVarialtionIds(product.getVarialtionIds());
		} finally {
			pm.close();
		}
		return true;
	}

	/**
	 * 
	 * @return
	 */
	public ProductSelectViewModel RetrievePrerequisites() {
		ProductSelectViewModel product = new ProductSelectViewModel();

		product.setBrands(new ManageBrandAction().Retrive());
		product.setCategories(new ManageCategoryAction().Retrive());

		return product;
	}

	/***
	 * 
	 * @return
	 */
	public List<ProductSelectViewModel> Retrieve() {
		PersistenceManager pm = Db.getPersistenceManager();
		Query query = pm.newQuery(Product.class);
		Object queryResult = query.execute();

		@SuppressWarnings("unchecked")
		List<Product> products = (List<Product>) queryResult;
		List<ProductSelectViewModel> productsView = new ArrayList<ProductSelectViewModel>();

		for (Product p : products) {
			ProductSelectViewModel product = new ProductSelectViewModel();
			product.setId(p.getKey().getId());
			product.setShortDescription(p.getShortDescription());
			product.setName(p.getName());
			product.setCostPrice(p.getCostPrice());
			productsView.add(product);
		}

		return productsView;
	}

	/***
	 * Paginated results
	 * 
	 * @param start
	 * @param length
	 * @return
	 */
	public List<ProductSelectViewModel> Retrieve(int start, int length) {
		PersistenceManager pm = Db.getPersistenceManager();
		Query query = pm.newQuery(Product.class);
		query.setRange(start, start + length);// start and end
		Object queryResult = query.execute();

		@SuppressWarnings("unchecked")
		List<Product> products = (List<Product>) queryResult;
		List<ProductSelectViewModel> productsView = new ArrayList<ProductSelectViewModel>();

		for (Product p : products) {
			ProductSelectViewModel product = new ProductSelectViewModel();
			product.setId(p.getKey().getId());
			product.setName(p.getName());
			product.setCostPrice(p.getCostPrice());
			productsView.add(product);
		}

		return productsView;
	}

	/***
	 * 
	 * @return
	 */
	public ProductSelectViewModel RetrieveVariations(long categoryId) {
		ProductSelectViewModel product = new ProductSelectViewModel();

		product.setVariations(new ManageVariationAction().Retrieve(categoryId));

		return product;
	}
}
